// 地区
import { findCount } from '@/api/ik/data/dataApi'
export const entityScript = {
  init: function(scriptJson) {
    // 判断是否存在dtms
    if (!Object.prototype.hasOwnProperty.call(scriptJson, 'drms')) {
      scriptJson.drms = {}
    }
    if (!Object.prototype.hasOwnProperty.call(scriptJson['drms'], 'area')) {
      scriptJson.drms.area = entityScript
    }
  },
  isinfoPop(code) {
    return false
  },
  // 按钮点击
  buttonClick: function(vueObject, row, btnEvent) {

  },
  infoButtonClick: function(vueObject, data, btnEvent) {
    if (btnEvent === 'save') {
      if (vueObject.id !== '') return false
      return this.save(vueObject, data)
    }
    if (btnEvent === 'update') {
      return this.update(vueObject, data)
    }
  },
  // 保存
  async save(vueObject, data) {
    if (data.head.name === '' || data.head.name === undefined || data.head.name === null) {
      vueObject.$message({
        type: 'warning',
        message: '项目名称未输入,请重新输入!'
      })
      // return true
      return Promise.resolve(true)
    }

    const res = await findCount(vueObject.model, vueObject.entity, {
      name: data.head.name
    })
    // let continute = true
    if (res !== 0) {
      vueObject.$message({
        message: '数据重复,请重新提交？',
        type: 'error',
        duration: 3000
      })
      return Promise.resolve(true)
    } else {
      return Promise.resolve(false)
    }
  },
  update(vueObject, data) {
    return false
  },
  // 表头样式初始化
  onInfoHeadFrameInit: function(vueObject, headFrameData) {
    //
  },
  // 表身样式初始化
  onInfoBodyFrameInit: function(vueObject, bodyFrameData) {
    //
  },
  // 表身新建行
  onAddBodyRow: function(vueObject, bodyName, itemData) {
    //
    //
  },
  // 表头改变事件
  onHeadChange: function(vueObject, data, fieldName, oldValue, newValue) {
    //
    //
    //
  },
  // 表身改变事件
  onBodyChange: function(vueObject, data, bodyName, lineNum, fieldName, oldValue, newValue) {
    //
    //
    //
    //
    //
  }
}
